package com.gasolinator.a.util;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

public class XmlXslParser
{
  private ApplicationData applicationData = null;

  public XmlXslParser() {
      this.applicationData = new ApplicationData();
  }

  public XmlXslParser(ApplicationData applicationDataMock) {
      this.applicationData = applicationDataMock;
  }

  public static String parse(String xmlContent, String xslContent)
           throws TransformerException, IOException
  {
        StringReader xmlContentSr = new StringReader(xmlContent);
        StringReader xslContentSr = new StringReader(xslContent);
        StringWriter result = new StringWriter();
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource(xslContentSr));
        transformer.transform( new javax.xml.transform.stream.StreamSource(xmlContentSr), new StreamResult(result));
        return result.toString();
  }

  public String parseAbout(String xmlContent, String xslContent)
          throws TransformerException, IOException, SAXException, ParserConfigurationException
  {
      xmlContent = addJavaVersionNode(xmlContent);
      return parse(xmlContent, xslContent);
  }

  public String addJavaVersionNode(String xmlContent)
          throws IOException, SAXException, ParserConfigurationException, TransformerException
  {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inputSource = new InputSource(new StringReader(xmlContent));
    Document doc = docBuilder.parse(inputSource);
    Node response = doc.getFirstChild();
    if (response.getNodeName().equals("response"))
    {
        Node javaVersion = doc.createElement("javaVersion");
        //TODO: change the jdk to real jdk, not the null openjdk, then I could use that function
        //javaVersion.setTextContent(this.applicationData.getSourceVersion());
        javaVersion.setNodeValue(this.applicationData.getSourceVersion());
        response.appendChild(javaVersion);

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "no");

        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

        xmlContent = result.getWriter().toString();
    }
    return xmlContent;
  }
}
